$(function() {
    var send_msg_rec_id = "";
    var baseUrl = "http://wuhan.pxteam.cn/ajax/app.aspx?method=";
    var timer;

    //点击发送短信验证码
    $("#sendCode").on("click", function() {
        sendCode();
    });
    //点击提交
    $(".submit").on("click", function() {
        var params = {
            real_name: $("#real_name").val(),
            phone: $("#phone").val(),
            valid_code: $("#valid_code").val(),
            password: $("#password").val(),
            confirm_password: $("#confirm_password").val(),
            referrer_user_id: $("#referrer_user_id").val(),
            send_msg_rec_id: send_msg_rec_id,
        };

        if (!params.real_name) {
            mtAlert("请输入姓名");
            return;
        }
        if (!params.phone) {
            mtAlert("请输入手机号");
            return;
        }
        if (!params.valid_code) {
            mtAlert("请输入验证码");
            return;
        }
        if (!params.password) {
            mtAlert("请输入密码");
            return;
        }
        if (!params.confirm_password) {
            mtAlert("请再次输入密码");
            return;
        }
        if (params.password != params.confirm_password) {
            mtAlert("两次密码不一致");
            return;
        }
        $.ajax({
            url: baseUrl + "app_Register",
            // datatype: 'json',
            type: "post",
            data: params,
            success: function(res) {
                var res = JSON.parse(res);
                console.log(res);
                if (res.IsSuccess == "1") {
                    mtAlert("注册成功,赶紧去下载app体验吧");
                    setTimeout(function() {
                        location.href = "./down.html";
                    }, 2000);
                } else {
                    mtAlert(res.ErrMsg);
                }
            },
        });
    });

    let userId = getQueryString("id");
    if (userId) {
        $("#referrer_user_id").val(userId);
    }

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function RegisterSendSms(params) {
        $.ajax({
            url: baseUrl + "SendSms",
            datatype: "json",
            type: "post",
            data: params,
            success: function(res) {
                var res = JSON.parse(res);
                console.log(res);
                if (res.IsSuccess == "1") {
                    send_msg_rec_id = res.send_msg_rec_id;
                    mtAlert("发送短信成功");
                } else {
                    mtAlert(res.ErrMsg);
                }
                $("#sendCode").html("获取验证码");
                $("#sendCode").removeClass("disabled");
                clearInterval(timer);
            },
        });
    }

    function sendCode() {
        if (!$("#phone").val()) {
            return mtAlert("请输入手机号");
        }

        RegisterSendSms({
            phone: $("#phone").val(),
        });
        $("#sendCode").addClass("disabled");
        let total = 30;
        $("#sendCode").html(total + "秒");
        timer = setInterval(() => {
            --total;
            $("#sendCode").html(total + "秒");
            if (total <= 0) {
                clearInterval(timer);
                $("#sendCode").html("获取验证码");
                $("#sendCode").removeClass("disabled");
            }
        }, 1000);
    }

    var mtAlert = function(_str, _time) {
        var sjc = new Date().getTime();
        $("body").append('<div id="alert_' + sjc + '" class="mt1alert">54545454</div>');
        $("#alert_" + sjc).html(_str);
        var _time = _time || 1500;
        setTimeout(function() {
            $("#alert_" + sjc).remove();
        }, _time);
    };
});